﻿package DCRS::Common;
#----------------------------------------------------------------------------
# Name:         Common.pm
# Purpose:      Support functions for DCRS Clarion web apps
# 
# Notes:        This module is part of the DCRS Dispatch Web Interface  
#
# Author:       Kingsley Wright
#
# Created:      2007-2008
# Copyright:    (c) 2008 by DCRS Solutions, Inc.
# Licence:      TBD - All rights reversed, DCRS Solutions, Inc.
#----------------------------------------------------------------------------
use Clarion;
use Time::CTime;
use DateTime::Format::Excel;

my $DBPATH = '/media/officedb/PUBLIC/OFFICE4/';

sub get_tech_names {
    # Build a hash of all techs using their assigned technumber as the key
    my $dbh   = new Clarion $DBPATH. 'svtech.dat';
    for (1 .. $dbh->last_record) {
        my @r = $dbh->get_record($_, 'NUMBER', 'NAME');
        next if $r[0];
        next if ($r[1] > 77);
        $tech_names{ $r[1] } = $r[2];
    }
    $dbh->close();
    $tech_names{0}   = "unassigned";
    $tech_names{all} = '- Include All Technicians -';
    return \%tech_names;
}

sub get_customer_list {
    my $dbh = new Clarion $DBPATH. 'custmr.dat';
    for (1 .. $dbh->last_record) {
        my @r = $dbh->get_record($_, 'NUMBER', 'SUB', 'NAME');
        next if $r[0];

        # Use account number and sub for key
        $customer_list{join(":", $r[1], $r[2])} = $r[3];
    }
    $customer_list{'all:all'} = '- Search All Accounts -';
    $dbh->close();
    return \%customer_list;
}

sub convert_date_to_clarion {
    # Takes the mdy values from localtime
    # and convert to Clarion long format date.
    my ($dt, $f);
    my @f = (localtime)[3 .. 5];    # grabs day/month/year values
    $dt = DateTime->new(
            year => $f[2] + 1900, month => $f[1] + 1, day => $f[0]);
    return (DateTime::Format::Excel->format_datetime($dt) + 36161);
}

sub convert_clarion_time_to_ampm {
    # Takes time value, serialized by number of seconds in 24 hour period.
    # Converts the serial time to human readable time.
    my $t = shift;

    my ($hour, $minute, $rtime, $am_pm, $open_time);

    $rtime  = ($t - 1) / 100;
    $hour   = $rtime / 3600;
    $minute = $rtime % 3600 / 60;
    $hour   = strftime(localtime($hour));
    $minute = strftime(localtime($minute));
    $am_pm  = 'am';
    $hour >= 12  and $am_pm  = 'pm';          # 12-23 are afternoon
    $hour > 12   and $hour   = $hour - 12;    # 13-23 ==> 1-11 (PM)
    $hour == 0   and $hour   = 12;            # convert first hour
    $minute < 10 and $minute = "0$minute";    # "5" ==> "05", etc.

    return "$hour:$minute$am_pm";
}

sub get_support_codes {
    # These are lables for our customer support status codes.
    my %supcodes = (
        O  => 'On Site',
        D  => 'Depot',
        N  => 'No Support',
        P  => 'SW/Helpdesk',
        R  => 'Rental',
        L  => 'Loaner',
        MA => 'Major Account',
        WK => 'Weekday',
        WW => 'Warranty Weekday',
        WD => 'Warranty Depot',
        WO => 'Warranty Onsite'
    );
    return \%supcodes;
}
1;